import {
	ArticleDetailResponse,
	ArticleCommentResponse,
	AddArticleCommnetResponse,
} from '@/types/data'
import { RootThunkAction } from '@/types/store'
import { http } from '@/utils/http'

// 导入 dayjs 相关包
import dayjs from 'dayjs'
import localizedFormat from 'dayjs/plugin/localizedFormat'
dayjs.extend(localizedFormat)

// 获取文章详情
export const getArticleById = (id: string): RootThunkAction => {
	return async (dispatch) => {
		const res = await http.get<ArticleDetailResponse>(`/articles/${id}`)

		dispatch({
			type: 'article/get',
			payload: {
				...res.data.data,
				// 注意：此处需要使用 本地化格式，才能展示：2019年3月11日
				pubdate: dayjs(res.data.data.pubdate).format('LL'),
			},
		})
	}
}

export const followAuthor = (
	id: string,
	isFollowed: boolean
): RootThunkAction => {
	return async (dispatch) => {
		if (isFollowed) {
			// 取关
			await http.delete(`/user/followings/${id}`)
		} else {
			// 关注
			await http.post('/user/followings', {
				target: id,
			})
		}

		dispatch({
			type: 'article/updateInfo',
			payload: {
				name: 'is_followed',
				value: !isFollowed,
			},
		})
	}
}

// 收藏 or 取消收藏文章
export const collectArticle = (
	art_id: string,
	is_collected: boolean
): RootThunkAction => {
	return async (dispatch) => {
		// 判断当前是否收藏
		if (is_collected) {
			// 当前是已收藏，此时，要取消收藏
			http.delete(`/article/collections/${art_id}`)
		} else {
			// 当前是未收藏，此时，要收藏
			http.post('/article/collections', {
				target: art_id,
			})
		}

		dispatch({
			type: 'article/updateInfo',
			payload: {
				name: 'is_collected',
				value: !is_collected,
			},
		})
	}
}

// 点赞 or 取消点赞文章
export const likeArticle = (
	art_id: string,
	attitude: number
): RootThunkAction => {
	return async (dispatch) => {
		// 判断当前是否点赞
		// 如果是 1 表示已点赞
		if (attitude === 1) {
			// 当前是点赞，此时，要取消点赞
			await http.delete(`/article/likings/${art_id}`)
		} else {
			// 当前是取消点赞，此时，要点赞
			await http.post('/article/likings', {
				target: art_id,
			})
		}

		dispatch({
			type: 'article/updateInfo',
			payload: {
				name: 'attitude',
				value: attitude === 1 ? 0 : 1,
			},
		})
	}
}

// 获取文章评论数据  // 第一次获取评论数据：覆盖数据
// type 用来指定获取的是 文章的评论 还是 评论的回复
// targetId 表示文章 或 评论的id
// offset 表示获取下一页的起始 id，对应接口返回数据的 last_id，
//           将来接口会根据该数据获取该 last_id 后面，也就是下一页的数据
export const getArticleComments = (
  type: string,
  targetId: string,
  offset: string | null
): RootThunkAction => {
  return async dispatch => {
    const res = await http.get<ArticleCommentResponse>('/comments', {
      params: {
        type,
        source: targetId,
        offset
      }
    })

    dispatch({
      type: 'article/getArticleComments',
      payload: res.data.data
    })
  }
}

// 第一次获取评论数据：覆盖数据
export const getArticleCommentsFirst = (
  type: string,
  targetId: string,
  offset: string | null
): RootThunkAction => {
  return async dispatch => {
    const res = await http.get<ArticleCommentResponse>('/comments', {
      params: {
        type,
        source: targetId,
        offset
      }
    })

    dispatch({
      type: 'article/getArticleCommentsFirst',
      payload: res.data.data
    })
  }
}

// 对文章发表评论
export const addArticleComment = (
	art_id: string,
	value: string
): RootThunkAction => {
	return async (dispatch) => {
		const res = await http.post<AddArticleCommnetResponse>('/comments', {
			content: value,
			target: art_id,
		})
		// console.log(res)

		dispatch({
			type: 'article/addArticleComment',
			payload: res.data.data.new_obj,
		})
	}
}

// 对评论进行点赞
export const articleCommentThumbUp = (
	com_id: string,
	is_liking: boolean
): RootThunkAction => {
	return async (disaptch) => {
		if (!is_liking) {
			// 说明：当前未点赞，此时，要点赞
			await http.post('/comment/likings', {
				target: com_id,
			})
		} else {
			// 说明：当前已点赞，此时，要取消点赞
			await http.delete(`/comment/likings/${com_id}`)
		}

		disaptch({
			type: 'artilce/articleCommentThumbUp',
			payload: {
				id: com_id,
				is_liking: !is_liking,
			},
		})
	}
}

// 更新某个评论项的回复数量
export const updateCommentCount = (
	commentId: string,
	total: number
): RootThunkAction => {
	return async (dispatch) => {
		dispatch({
			type: 'article/updateCommentCount',
			payload: {
				commentId,
				total,
			},
		})
	}
}
